package com.chpro.scsw.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.chpro.scsw.dao.PcBeseDao;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.chpro.common.HttpUtil;
public class PcBeseDaoImpl implements PcBeseDao {

	public List<Map<String, String>> queryList(String url,Integer firstPage,Integer lastPage,String contentRegular,Map map) {
		List<Map<String,String>> result = new ArrayList<Map<String,String>>();

		String uri = url;
		
		for(int i=firstPage;i<=lastPage;i++){
			String html = HttpUtil.httpGet(uri+i);	//获取文章列表页面
	
			Document doc = Jsoup.parse(html);//解析HTML字符串返回一个Document实现 
			Elements links = doc.select(contentRegular);
			for(Element link:links){
				Map temp = new HashMap<String,String>();
				
				Object[] param = map.keySet().toArray();
				for(Object obj:param){
					temp.put(obj,link.select(map.get(obj).toString()).eq(0).attr("src"));
				}
				
				result.add(temp);
				
			}
		}
		return result;
	}

	public List<Map<String, String>> queryListDetail() {
		
		return null;
	}

	public Integer queryPageCount(String url,String regular) {
		String result = "0";
		
		String uri = url;
		String html = HttpUtil.httpGet(uri);	//获取文章列表页面
		Document doc = Jsoup.parse(html);//解析HTML字符串返回一个Document实现 
		String a = doc.select(regular).last().attr("href");
		
		String str = a;
		String s = "\\d+";
		Pattern pattern = Pattern.compile(s);  
		Matcher ma = pattern.matcher(str);
		if(ma.find()){
			result = ma.group();
		}
		
		return Integer.parseInt(result);
	}

}
